#include "gamescene.h"
#include <QPainter>
#include <QPixmap>
#include <QLabel>
#include <QPropertyAnimation>
#include "gamepushbutton.h"
#include "gamecontroller.h"
#include "gameconfig.h"
#include "dataconfig.h"
GameScene::GameScene(int level,QWidget *parent) : QMainWindow(parent)
  ,levelId(level)
{
    this->setFixedSize(GAME_WIDTH,GAME_HEIGHT);
    this->setWindowTitle("游戏界面");
    this->setWindowIcon(QIcon(GAME_ICON));
    //游戏胜利标志
    m_lblWinGame = new QLabel(this);
    QPixmap winPixmap(IMG_LEVEL_COMPLETE);
    m_lblWinGame->setPixmap(winPixmap);
    m_lblWinGame->setGeometry(0,0,winPixmap.width(),winPixmap.height());
    m_lblWinGame->move((GAME_WIDTH-m_lblWinGame->width())/2,-100);
    //设置返回按钮（到关卡选择的界面）
    GamePushButton *btnBack = new GamePushButton(BTN_BACK_NORMAL,BTN_BACK_PRESSED);
    btnBack->setParent(this);
    btnBack->move(GAME_WIDTH - btnBack->width(),GAME_HEIGHT - btnBack->height());
    connect(btnBack,&GamePushButton::clicked,this,[=](){
        GameController::getInstance()->showLevelScene();
        GameController::getInstance()->deleteGameScene();

        GameController::getInstance()->playBackSound();
    });
    //游戏关卡标志设置
    QLabel* label = new QLabel(this);
    label->setText(QString("LEVEL-%1").arg(levelId));
    label->setGeometry(10,GAME_HEIGHT - 80,180,50);
    QFont levelFont;
    levelFont.setFamily("黑体");
    levelFont.setBold(true);
    levelFont.setPointSize(20);
    label->setFont(levelFont);
    //1游戏设置
    DataConfig gameDataInfo;
    for(int i = 0; i < 4; i++){
        for (int j = 0; j < 4 ; j++ ) {

            m_gameInfoArray[i][j] = gameDataInfo.mData[levelId][i][j];

            //游戏背景
            QPixmap pix = QPixmap(IMG_GAME_BACKGROUND);
            QLabel* label = new QLabel(this);
            label->setGeometry(0,0,pix.width(),pix.height());
            label->setPixmap(pix);
            label->move(95 + i*50,200+j*50);
            //金币初始化
            QString strCoin = "";
            if(m_gameInfoArray[i][j] == 1){
                strCoin = ":/res/Coin0001.png";
            } else {
                strCoin = ":/res/Coin0008.png";
            }
            m_btnCoin[i][j] = new GameCoin(strCoin,label);
//            m_btnCoin[i][j]->move(96 + i*50,201+j*50);
            m_btnCoin[i][j]->move(2,3);
            m_btnCoin[i][j]->setPosX(i);
            m_btnCoin[i][j]->setPosY(j);
            m_btnCoin[i][j]->setFlag(m_gameInfoArray[i][j]);

            //金币的点击事件
            connect(m_btnCoin[i][j],&QPushButton::clicked,this,[=](){
               m_gameInfoArray[i][j] = m_gameInfoArray[i][j] == 1 ? 0:1;

               m_btnCoin[i][j]->changeFlag();
               GameController::getInstance()->playFlipCoinSound();
               QTimer::singleShot(30,this,[=](){
//                   m_btnCoin[i][j]->setIsAnimation(true);
                   int xIndex = m_btnCoin[i][j]->posX();
                   int yIndex = m_btnCoin[i][j]->posY();
                   //因为二维数组在画图时，相当于基于x，y坐标系，ij相当于交换了，本来
                   //i代表行，现在j代表行 == y，j代表列，现在i代表列，==x
                   //左边
                   if(xIndex > 0){
                       m_gameInfoArray[xIndex - 1][yIndex] = m_gameInfoArray[xIndex - 1][yIndex] ==1 ? 0 :1;
                       m_btnCoin[xIndex - 1][yIndex]->changeFlag();
                   }
                   //右边
                   if(xIndex < 3){
                       m_gameInfoArray[xIndex + 1][yIndex] = m_gameInfoArray[xIndex + 1][yIndex] ==1 ? 0 :1;
                       m_btnCoin[xIndex + 1][yIndex]->changeFlag();
                   }
                   //上面
                   if(yIndex > 0){
                       m_gameInfoArray[xIndex][yIndex - 1] = m_gameInfoArray[xIndex][yIndex - 1] ==1 ? 0 :1;
                       m_btnCoin[xIndex][yIndex - 1]->changeFlag();
                   }
                   //下面
                   if(yIndex < 3){
                       m_gameInfoArray[xIndex][yIndex + 1] = m_gameInfoArray[xIndex][yIndex + 1] ==1 ? 0 :1;
                       m_btnCoin[xIndex][yIndex + 1]->changeFlag();
                   }
                   bool isWin = true;
                   for (int m = 0; m < 4 ; m++ ) {
                       for(int n = 0; n < 4;n++){
                           if(m_gameInfoArray[m][n]==0){
                               isWin = false;
                               break;
                           }
                       }
                   }
                   if(isWin == true){
//                       qDebug()<<"恭喜你，过关了";
//                       GameController::getInstance()->playWinSound();
                       GameController::getInstance()->playWinSound();
                      //胜利动画
                          QPropertyAnimation *winAnimation=new QPropertyAnimation(m_lblWinGame,"geometry");
                          winAnimation->setDuration(1000);
                          winAnimation->setStartValue(QRect(m_lblWinGame->x(),m_lblWinGame->y()
                                                            ,m_lblWinGame->width(),m_lblWinGame->height()));
                          winAnimation->setEndValue(QRect(m_lblWinGame->x(),40
                                                          ,m_lblWinGame->width(),m_lblWinGame->height()));
                          winAnimation->setEasingCurve(QEasingCurve::OutBounce);
                          winAnimation->start();
                         //设置金币不可点击
                       for(int m = 0; m < 4;m++){
                           for(int n = 0; n < 4;n++){
                               m_btnCoin[m][n]->setIsWin(true);
                           }
                       }

                   }
                   });
            });
        }
    }
}



//设置游戏背景
void GameScene::paintEvent(QPaintEvent *event){
    QPainter gamePaint(this);
    QPixmap gamePixmap;
    bool ret = gamePixmap.load(IMG_BG_GAMESCENE);
    if(ret){
        gamePaint.drawPixmap(0,0,gamePixmap);
    }
    //设置标题图标
    gamePixmap.scaled(gamePixmap.width() * 0.7,gamePixmap.height() * 0.7);
    ret = gamePixmap.load(IMG_GAME_TITLE);
    gamePaint.drawPixmap((GAME_WIDTH - gamePixmap.width())*0.5,30,gamePixmap);
}
